<?php

namespace app\api\controller\shop;


use app\admin\model\Notice;
use app\admin\model\Org;
use app\admin\model\Shopcharge;
use app\admin\model\Shopchargeconfig;
use app\admin\model\Shopdevice;
use app\admin\model\Shopmno;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproomset;
use app\admin\model\Shoptuan;
use app\admin\model\Shopwifi;
use app\admin\model\Tuanorder;
use app\admin\model\Userbalance;
use app\admin\model\Usertuan;
use app\api\service\VbillService;
use app\common\controller\Api;
use fast\Http;
use OSS\OssClient;
use think\Config;

/**
 *
 */
class Shop extends Api
{
    protected $noNeedLogin = ['getNotice'];
    protected $noNeedRight = ['*'];

    /**
     * 店铺列表接口
     *
     */
    public function getList()
    {
        $user = $this->auth->getUser();

        $keyword = $this->request->param('keyword');
        $where = array();
        $where['user_id'] = $user['id'];
        if ($keyword != '') {
            $where['name'] = array('like', "'%" . $keyword . "%'");
        }
        $shopModel = new \app\admin\model\Shop();
        $list = $shopModel->where($where)->select();
        foreach ($list as $key => $val) {
            $list[$key]['logo'] = cdnurl($val['logo'], true);
        }
        $this->success('请求成功', $list);
    }

    /**
     * wifi设置
     */
    public function wifiSet()
    {

        $shop_id = $this->request->param('shop_id');
        $name = $this->request->param('name');
        $password = $this->request->param('password');

        $db = array();
        $db['wifi_name'] = $name;
        $db['wifi_password'] = $password;
        $shopWifiModel = new Shopwifi();
        $wifi = $shopWifiModel->where('shop_id', $shop_id)->find();
        if ($wifi) {
            $shopWifiModel->where('id', $wifi['id'])->update($db);
        } else {
            $db['shop_id'] = $shop_id;
            $shopWifiModel->insert($db);
        }
        $this->success('成功');
    }

    /**
     * 获取wifi信息
     */
    public function getShopWifi()
    {
        $shop_id = $this->request->param('shop_id');
        $shopWifiModel = new Shopwifi();
        $wifi = $shopWifiModel->where('shop_id', $shop_id)->find();
        $this->success('', $wifi);
    }

    /**
     * 充值记录
     */
    public function getChargeList()
    {

        $shop_id = $this->request->param('shop_id');
        $mobile = $this->request->param('mobile');
        $startTime = $this->request->param('startTime');
        $endTime = $this->request->param('endTime');

        $page = $this->request->param('page');
        $shopChargeModel = new Shopcharge();
        $where = array();
        $where['shop_id'] = $shop_id;
        $where2 = array();
        $where2['shop_id'] = $shop_id;

        if ($mobile != '') {
            $where['User.mobile'] = $mobile;
            $userModel = new \app\admin\model\User();
            $u = $userModel->where('mobile', $mobile)->find();
            $where2['user_id'] = $u['id'];

        }


        if ($startTime != '' && $endTime != '') {

            $start = strtotime($startTime . " 00:00:00");
            $end = strtotime($endTime . " 23:59:59");
            $where['create_time'] = array('between', array($start, $end));
            $where2['create_time'] = array('between', array($start, $end));
        }

        $payAmount = $shopChargeModel->with(['shop', 'user'])->where('Shopcharge.status', 'payed')->where($where)->order('create_time', 'desc')->sum('amount');
        $totalAmount = $shopChargeModel->with(['shop', 'user'])->where('Shopcharge.status', 'payed')->where($where)->order('create_time', 'desc')->sum('get_amount');

//        $where2['status'] = array('not in', ['paying', 'refund']);
//
//        $shopOrderModel = new Shoporder();
//        $totalBalance = $shopOrderModel->where($where)->sum('balance');


        $userBalanceModel = new Userbalance();
        $in = $userBalanceModel->where('shop_id',$shop_id)->where('type','in')->sum('amount');
        $out = $userBalanceModel->where('shop_id',$shop_id)->where('type','out')->sum('amount');


        $ret = array();
        $ret['payAmount'] = $payAmount;
        $ret['sendAmount'] = $totalAmount - $payAmount;
        $ret['unAmount'] = formatMoney($in - $out);


        $limit = 10;
        $offset = ($page - 1) * $limit;

        $list = $shopChargeModel->with(['shop', 'user'])->where('Shopcharge.status', 'payed')->where($where)->order('create_time', 'desc')->limit($offset, $limit)->select();

        foreach ($list as $key => $val) {
            $val['user']->hidden(['password', 'salt']);
            $list[$key]['create_time_txt'] = date("Y-m-d H:i", $val['create_time']);
        }
        $ret['list'] = $list;
        $this->success('', $ret);

    }


    /**
     *门店详情
     */
    public function shopDetail()
    {

        $shop_id = $this->request->param('shop_id');

        $shopModel = new \app\admin\model\Shop();
        $shop = $shopModel->where('id', $shop_id)->find();
        $shop['logo_url'] = cdnurl($shop['logo'], true);

        $userModel = new \app\admin\model\User();
        $user = $userModel->where('id', $shop['user_id'])->find();
        $shop['username'] = $user['username'];
        if ($shop['tag'] != "") {
            $tags = explode(",", $shop['tag']);

        } else {
            $tags = [];

        }
        $coverList = array();
        if ($shop['cover'] != '') {
            $covers = explode(",", $shop['cover']);

            foreach ($covers as $key => $val) {
                $obj = array();
                $obj['cover'] = $val;
                $obj['cover_url'] = cdnurl($val, true);
                $coverList[] = $obj;
            }
        }

        $shop['coverList'] = $coverList;
        $shop['tagList'] = $tags;
        $shop['status_txt'] = $shopModel->getStatus($shop['status']);

        if ($shop['is_clean_book'] == 1) {
            $shop['clean_txt'] = "可预订";
        } else {
            $shop['clean_txt'] = "不可预订";
        }

        $shopDeviceModel = new Shopdevice();
        $device1 = $shopDeviceModel->where('shop_id', $shop_id)->where('room_id', -1)->find();
        $device2 = $shopDeviceModel->where('shop_id', $shop_id)->where('room_id', -2)->find();
        $shop['device1'] = $device1;
        $shop['device2'] = $device2;


        $this->success('', $shop);
    }


    public function updateDetail()
    {

        $shop_id = $this->request->param('shop_id');
        $name = $this->request->param('name');
        $logo = $this->request->param('logo');
        $status = $this->request->param('status');
        $cover = $this->request->param('cover');
        $tel = $this->request->param('tel');
        $address = $this->request->param('address');
        $lat = $this->request->param('lat');
        $lng = $this->request->param('lng');
        $tag = $this->request->param('tag');
        $isCleanBookStatus = $this->request->param('is_clean_book');
        $is_charge_tip = $this->request->param('is_charge_tip');
        $wx_code = $this->request->param('wx_code');
        $shopModel = new \app\admin\model\Shop();
        $shop = $shopModel->where('id', $shop_id)->find();
        $db = array();
        if($name != '' && $name != $shop['name']){
            $this->error('不能修改店铺名称');
        }
        if($wx_code != ""){
            $db['wx_code'] = $wx_code;
        }
        if ($logo != '') {
            $db['logo'] = $logo;
        }
        if ($isCleanBookStatus != '') {
            $db['is_clean_book'] = $isCleanBookStatus;
        }
        if($is_charge_tip != ''){
            $db['is_charge_tip'] = $is_charge_tip;
        }

        if ($status != '') {
            $db['status'] = $status;
        }
        if ($tel != '') {
            $db['tel'] = $tel;
        }
        if ($address != '') {
            $db['address'] = $address;
            $db['lat'] = $lat;
            $db['lng'] = $lng;
        }
        if ($tag != '') {
            if ($shop['tag'] != "") {
                $tagList = explode(",", $shop['tag']);
            } else {
                $tagList = [];
            }
            $isAdd = true;
            foreach ($tagList as $key => $val) {
                if ($val == $tag) {
                    $isAdd = false;
                }
            }
            if ($isAdd) {
                $tag = $shop['tag'] . "," . $tag;
                $db['tag'] = $tag;
            }

        }


        if ($cover != '') {
            $str = $shop['cover'] . "," . $cover;
            $db['cover'] = $str;
        }
        $shopModel->where('id', $shop_id)->update($db);
        $this->success('成功');
    }

    public function removeTag()
    {

        $shop_id = $this->request->param('shop_id');

        $tagStr = $this->request->param('tag');

        $shopModel = new \app\admin\model\Shop();
        $shop = $shopModel->where('id', $shop_id)->find();

        $tags = explode(",", $shop['tag']);
        $tagList = array();
        foreach ($tags as $key => $val) {
            if ($val != $tagStr) {
                $tagList[] = $val;
            }
        }
        $str = "";
        foreach ($tagList as $key => $val) {
            $str = $str . $val;
            if ($key != count($tagList) - 1) {
                $str = $str . ",";
            }
        }
        $db = array();
        $db['tag'] = $str;
        $shopModel->where('id', $shop_id)->update($db);
        $this->success('成功');
    }


    /**
     * 移除封面
     */
    public function removeCover()
    {
        $shop_id = $this->request->param('shop_id');
        $coverStr = $this->request->param('cover');

        $shopModel = new \app\admin\model\Shop();
        $shop = $shopModel->where('id', $shop_id)->find();

        $cover = explode(",", $shop['cover']);
        $covers = array();
        foreach ($cover as $key => $val) {
            if ($val != $coverStr) {
                $covers[] = $val;
            }
        }
        $str = "";
        foreach ($covers as $key => $val) {
            $str = $str . $val;
            if ($key != count($covers) - 1) {
                $str = $str . ",";
            }
        }
        $db = array();
        $db['cover'] = $str;
        $shopModel->where('id', $shop_id)->update($db);
        $this->success('成功');

    }


    /**
     * 团购券
     */
    public function tuanList()
    {

        $shop_id = $this->request->param('shop_id');
        $shopTuanModel = new Shoptuan();
        $list = $shopTuanModel->where('shop_id', $shop_id)->where('status', 1)->select();
        foreach ($list as $key => $val) {
            $list[$key]['check'] = false;
            $tag = explode("|", $val['rule_txt']);
            $tags = array();
            foreach ($tag as $k => $v) {
                $obj = array();
                $obj['id'] = ($k + 1);
                $obj['value'] = $v;
                $tags[] = $obj;
            }
            $list[$key]['rule_txt'] = $tags;
        }
        $this->success('', $list);
    }

    /**
     * 作废
     */
    public function closeTuan()
    {
        $user = $this->auth->getUser();
        $tuan_id = $this->request->param('tuan_id');
        $shopTuanModel = new Shoptuan();
        $tuan = $shopTuanModel->where('id', $tuan_id)->find();
        if (!$tuan) {
            $this->error("不存在此店铺的团购券");
        }
        $shopModel = new \app\admin\model\Shop();
        $shop = $shopModel->where('user_id', $user['id'])->where('id', $tuan['shop_id'])->find();
        if (!$shop) {
            $this->error("无权限操作");
        }

        $shopTuanModel->where('id', $tuan_id)->update(['status' => -1]);

        $this->success('成功');
    }

    public function addTuan()
    {
        $shopTuanModel = new Shoptuan();
        $shop_id = $this->request->param('shop_id');
        $name = $this->request->param('name');
        $startTime = $this->request->param('startTime');
        $endTime = $this->request->param('endTime');
        $num = $this->request->param('num');
        $valid_day = $this->request->param('valid_day');
        $price = $this->request->param('price');
        $line_price = $this->request->param('line_price');
        $hour = $this->request->param('hour');
        $room = $this->request->param('room');
        $week = $this->request->param('week');
        $notice = $this->request->param('notice');


        $room = json_decode(htmlspecialchars_decode($room), true);
        $week = json_decode(htmlspecialchars_decode($week), true);
        $notice = json_decode(htmlspecialchars_decode($notice), true);

        $time = $startTime . "~" . $endTime;
        $w = "";
        $newWeek = array();
        foreach ($week as $key => $val) {
            $w .= $val['name'];
            if ($key != count($week) - 1) {
                $w .= ",";
            }
            if ($val['name'] == "一") {
                $newWeek[] = 1;
            }
            if ($val['name'] == "二") {
                $newWeek[] = 2;
            }
            if ($val['name'] == "三") {
                $newWeek[] = 3;
            }
            if ($val['name'] == "四") {
                $newWeek[] = 4;
            }
            if ($val['name'] == "五") {
                $newWeek[] = 5;
            }
            if ($val['name'] == "六") {
                $newWeek[] = 6;
            }
            if ($val['name'] == "日") {
                $newWeek[] = 7;
            }
        }

        $notice1 = "该团购券" . $w . $time;
        foreach ($notice as $key => $val) {
            $notice1 .= $val['name'];
            if ($key != count($notice) - 1) {
                $notice1 .= "|";
            }
        }

        $obj = array();
        $obj['time'] = $time;
        $obj['week'] = $newWeek;
        $rooms = array();
        foreach ($room as $key => $val) {

            $rooms[] = $val['id'];
        }
        $shops = array();
        $shopArray = array();
        $shopArray['shop_id'] = $shop_id;
        $shopArray['room'] = $rooms;
        $shops[] = $shopArray;
        $obj['shop'] = $shops;


        $db = array();
        $db['name'] = $name;
        $db['price'] = $price;
        $db['line_price'] = $line_price;
        $db['shop_id'] = $shop_id;
        $db['valid_day'] = $valid_day;
        $db['hour'] = $hour;
        $db['is_max_buy'] = $num;
        $db['status'] = 1;
        $db['rule_txt'] = $notice1;
        $db['rule'] = json_encode($obj);
        $db['create_time'] = time();

        $id = $shopTuanModel->insertGetId($db);
        if ($id) {
            $this->success("添加成功");
        } else {
            $this->error("添加失败");
        }

    }

    /**
     * 团购券购买记录
     */
    public function tuanBuyList()
    {

        $page = $this->request->param('page');
        $shop_id = $this->request->param('shop_id');
        $shopTuanOrderModel = new Tuanorder();
        $startTime = $this->request->param('startTime');
        $endTime = $this->request->param('endTime');
        $mobile = $this->request->param('mobile');

        $where = array();
        $where['Tuanorder.shop_id'] = $shop_id;
        $where2 = array();
        $where2['Tuanorder.shop_id'] = $shop_id;


        $totalAmount = $shopTuanOrderModel->with(['tuan', 'user'])->where('Tuanorder.status', 'payed')->where($where)->sum('amount');
        $refundAmount = $shopTuanOrderModel->with(['tuan', 'user'])->where('Tuanorder.status', 'refund')->where($where)->sum('amount');


        if ($mobile != '') {
            $where['User.mobile'] = $mobile;
            $userModel = new \app\admin\model\User();
            $u = $userModel->where('mobile', $mobile)->find();
            $where2['user_id'] = $u['id'];

        }


        if ($startTime != '' && $endTime != '') {

            $start = strtotime($startTime . " 00:00:00");
            $end = strtotime($endTime . " 23:59:59");
            $where['Tuanorder.create_time'] = array('between', array($start, $end));
            $where2['Tuanorder.create_time'] = array('between', array($start, $end));
        }


        $ret = array();


        $limit = 10;
        $offset = ($page - 1) * $limit;

        $list = $shopTuanOrderModel->with(['tuan', 'user', 'used'])->where('Tuanorder.status', 'payed')->where($where)->order('Tuanorder.create_time', 'desc')->limit($offset, $limit)->select();
        foreach ($list as $key => $val) {
            $list[$key]['valid'] = date("Y-m-d H:i", strtotime("+" . ($val['tuan']['valid_day'] * 24) . " hour", $val['create_time']));
        }
        $ret['list'] = $list;
        $ret['total'] = $totalAmount;
        $ret['refund'] = $refundAmount;

        $this->success('', $ret);


    }


    /**
     * 配置
     */
    public function configList()
    {

        $shop_id = $this->request->param('shop_id');
        $shopConfigModel = new Shopchargeconfig();
        $list = $shopConfigModel->where('shop_id', $shop_id)->order('amount', 'asc')->select();

        $this->success('', $list);
    }

    public function removeConfigItem()
    {
        $id = $this->request->param('id');
        $shopConfigModel = new Shopchargeconfig();
        $shopConfigModel->where('id', $id)->delete();
        $this->success('移除成功');
    }

    public function addConfigSet()
    {

        $shop_id = $this->request->param('id');
        $set = $this->request->param('set');

        $shopRoomSetModel = new Shopchargeconfig();


        $shopModel = new \app\admin\model\Shop();
        $shop = $shopModel->where('id',$shop_id)->find();
        $org_id = $shop['org_id'];
        $set = json_decode(htmlspecialchars_decode($set), true);

        $db = array();
        foreach ($set as $key => $val) {
            $obj = array();


            if($org_id == 53 || $org_id== 27){
                if($val['get_amount'] > $val['amount']*2){

                    $this->error("赠送金额不能大于充值金额的2倍");
                    break;
                } else{
                    if ($val['id'] == -1) {

                        $obj['shop_id'] = $shop_id;
                        $obj['amount'] = $val['amount'];
                        $obj['get_amount'] = $val['get_amount'];
                        $obj['create_time'] = time();

                        $shopRoomSetModel->insert($obj);
                    } else {
                        $obj['shop_id'] = $shop_id;
                        $obj['amount'] = $val['amount'];
                        $obj['get_amount'] = $val['get_amount'];
                        $obj['create_time'] = time();
                        $shopRoomSetModel->where('id', $val['id'])->update($obj);

                    }
                }
            }else{
                if ($val['id'] == -1) {

                    $obj['shop_id'] = $shop_id;
                    $obj['amount'] = $val['amount'];
                    $obj['get_amount'] = $val['get_amount'];
                    $obj['create_time'] = time();

                    $shopRoomSetModel->insert($obj);
                } else {
                    $obj['shop_id'] = $shop_id;
                    $obj['amount'] = $val['amount'];
                    $obj['get_amount'] = $val['get_amount'];
                    $obj['create_time'] = time();
                    $shopRoomSetModel->where('id', $val['id'])->update($obj);

                }
            }




        }
        $this->success('成功');
    }


    public function tuanRefund()
    {

        $id = $this->request->param('id');
        $userTuanModel = new Usertuan();
        $userTuan = $userTuanModel->where('order_id', $id)->find();
        if (!$userTuan) $this->error("此券不可退款");
        if ($userTuan['status'] == 'used') $this->error("此券已使用,不可退款");


        $tuanOrderModel = new Tuanorder();
        $order = $tuanOrderModel->where('id', $id)->find();


        $orgModel = new Org();
        $org = $orgModel->where('id', $order['org_id'])->find();
        $mno = $org['mno'];
        $shopMnoModel = new Shopmno();
        $shopMno = $shopMnoModel->where('shop_id',$order['shop_id'])->find();
        if($shopMno && $shopMno['ratio'] == 100){
            $mno = $shopMno['mno'];
        }

        $refundOrder_no = getOrderSn();
        $req = array();
        $req['mno'] =$mno;
        $req['ordNo'] = $refundOrder_no;
        $req['origOrderNo'] = $order['order_no'];
        $req['amt'] = $order['amount'];

        $vbillService = new VbillService();
        $ret = $vbillService->refund($req);
        error_log("===tuan refund=req=".json_encode($req));

        error_log("===tuan refund==".$ret);

        $result = json_decode($ret, true);
        if ($result['respData']['bizCode'] != "0000") {
            $this->error($result['respData']['bizMsg']);
        }
        $tuanOrderModel->where('id', $id)->update(['status' => 'refund']);
        $userTuanModel->where('id', $userTuan['id'])->update(['status' => 'refund']);

        $shopProfitModel = new Shoporderprofit();
        $shopProfitModel->where('order_id',$order['id'])->where('type','tuan')->where('from','qp')->delete();
        $this->success('成功');


    }


    public function getShopCodeUrl()
    {

        $user = $this->auth->getUser();
        $shop_id = $this->request->param('shop_id');

        $shopModel = new \app\admin\model\Shop();
        $shop = $shopModel->where('id', $shop_id)->find();

        $orgModel = new Org();
        $org = $orgModel->where('id', $shop['org_id'])->find();

        $app_id = $org['app_id'];
        $app_secret = $org['app_secret'];


        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $app_id . "&secret=" . $app_secret;
        $ret = Http::get($url);
        $obj = json_decode($ret, true);
        $token = $obj['access_token'];


        $scene = $shop['id'] . "-1";
        error_log("==scene===" . $scene);
        $shop_id = $this->request->param('shop_id');

        $url2 = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$token";
        $data = [
            'scene' => $scene,
            'page' => 'pages/shop/detail',

        ];
        $name = "qx_shop_" . $shop['id'];
        $data = json_encode($data);
        //拿到二维码
        $result = Http::post($url2, $data);
        $base64 = base64_encode($result);
        $mime = "image/png";
        $img = 'data:' . $mime . ';base64,' . $base64;
        $filename = './uploads/' . $name . '.jpg';

        $qrcodePath = ROOT_PATH . 'public/uploads/qrcode/';
        if (!is_dir($qrcodePath)) {
            @mkdir($qrcodePath);
        }
        if ($shop['qr_code'] != '') {
            $url = cdnurl($shop['qr_code'], true);
            $this->success('', $url);
        } else {
            base64_image_content($img, $filename);

            $per = 0.5;
            list($width, $height) = getimagesize($filename);
            $n_w = $width * $per;
            $n_h = $height * $per;
            $new = imagecreatetruecolor($n_w, $n_h);
            $img = imagecreatefromjpeg($filename);
//拷贝部分图像并调整
            imagecopyresized($new, $img, 0, 0, 0, 0, $n_w, $n_h, $width, $height);
//图像输出新图片、另存为
            imagedestroy($new);
            imagedestroy($img);

            $config = get_addon_config('alioss');
            $oss = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endpoint']);
            $ret = $oss->uploadFile($config['bucket'], $name . ".jpg", $filename);
            $url = $ret['info']['url'];

            $shop->where('id', $shop_id)->update(['qr_code' => "/" . $name . ".jpg"]);

            $url = str_replace("http", "https", $url);
            $this->success('', $url);
        }


    }



    public function getChargeCodeUrl()
    {

        $user = $this->auth->getUser();
        $shop_id = $this->request->param('shop_id');

        $shopModel = new \app\admin\model\Shop();
        $shop = $shopModel->where('id', $shop_id)->find();

        $orgModel = new Org();
        $org = $orgModel->where('id', $shop['org_id'])->find();

        $app_id = $org['app_id'];
        $app_secret = $org['app_secret'];


        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $app_id . "&secret=" . $app_secret;
        $ret = Http::get($url);
        $obj = json_decode($ret, true);
        $token = $obj['access_token'];


        $scene = $shop['id'] . "-1";
        error_log("==scene===" . $scene);
        $shop_id = $this->request->param('shop_id');

        $url2 = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$token";
        $data = [
            'scene' => $scene,
            'page' => 'pages/discount/charge',

        ];
        $name = "qx_shop_charge_" . $shop['id'];
        $data = json_encode($data);
        //拿到二维码
        $result = Http::post($url2, $data);
        $base64 = base64_encode($result);
        $mime = "image/png";
        $img = 'data:' . $mime . ';base64,' . $base64;
        $filename = './uploads/' . $name . '.jpg';

        $qrcodePath = ROOT_PATH . 'public/uploads/qrcode/';
        if (!is_dir($qrcodePath)) {
            @mkdir($qrcodePath);
        }
        if ($shop['charge_qr_code'] != '') {
            $url = cdnurl($shop['charge_qr_code'], true);
            $this->success('', $url);
        } else {
            base64_image_content($img, $filename);

            $per = 0.5;
            list($width, $height) = getimagesize($filename);
            $n_w = $width * $per;
            $n_h = $height * $per;
            $new = imagecreatetruecolor($n_w, $n_h);
            $img = imagecreatefromjpeg($filename);
//拷贝部分图像并调整
            imagecopyresized($new, $img, 0, 0, 0, 0, $n_w, $n_h, $width, $height);
//图像输出新图片、另存为
            imagedestroy($new);
            imagedestroy($img);

            $config = get_addon_config('alioss');
            $oss = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endpoint']);
            $ret = $oss->uploadFile($config['bucket'], $name . ".jpg", $filename);
            $url = $ret['info']['url'];

            $shop->where('id', $shop_id)->update(['charge_qr_code' => "/" . $name . ".jpg"]);

            $url = str_replace("http", "https", $url);
            $this->success('', $url);
        }


    }



    public function addNotice()
    {

        $id = $this->request->param('id');
        $content = $this->request->param('content');
        $title = $this->request->param('title');
        $content = json_decode(htmlspecialchars_decode($content), true);

        $str = "";

        $contents = array();
        foreach ($content as $key => $val) {
            if (empty($val['content']) != 1) {
                $contents[] = $val['content'];
            }
        }

        error_log("===contents==".json_encode($contents));
        foreach ($contents as $key => $val) {

            $str = $str . $val;
            if ($key != count($contents) - 1) {
                $str = $str . ",";
            }


        }

        $noticeModel = new Notice();
        $notice = $noticeModel->where('shop_id', $id)->find();
        if ($notice) {
            $noticeModel->where('id', $notice['id'])->update(['title' => $title, 'create_time' => time(), 'shop_id' => $id, 'content' => $str]);

        } else {
            $noticeModel->insert(['title' => $title, 'create_time' => time(), 'shop_id' => $id, 'content' => $str]);

        }

        $this->success('成功');
    }


    public function balanceList()
    {

        $mobile = $this->request->param('mobile');
        $userModel = new \app\admin\model\User();
        $user = $userModel->where('mobile', $mobile)->find();

        $userBalanceModel = new Userbalance();
        $in = $userBalanceModel->where('type', 'in')->where('user_id', $user['id'])->sum('amount');
        $out = $userBalanceModel->where('type', 'out')->where('user_id', $user['id'])->sum('amount');
        $list = $userBalanceModel->where('user_id', $user['id'])->order('create_time', 'desc')->select();

        $ret = array();
        $ret['balance'] = formatMoney($in - $out);
        $ret['list'] = $list;
        $this->success('', $ret);
    }

    public function setTuanUse(){

        $id = $this->request->param('id');
        $userTuanModel = new Usertuan();
        $userTuanModel->where('id',$id)->update(['status'=>'used']);
        $this->success('成功');
    }


}
